<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="/sys/base :: headFragment(~{::title},~{::link},~{::style})"></head>
<body>
<div class="layui-form base-dialog">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="userName" name="userName" lay-verify="required|userName" placeholder="请输入姓名" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">账号</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="account" name="account" lay-verify="required|account" placeholder="请输入账号" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-inline">
                <input type="password" class="layui-input" id="pass" name="pass" lay-verify="required|pass" placeholder="请输入密码" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" class="layui-input" id="rePass" name="rePass" lay-verify="required|rePass" placeholder="请输入密码" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机号码</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="phone" name="phone" lay-verify="required|phone" placeholder="请输入手机号码" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="email" name="email" lay-verify="required|email" placeholder="请输入邮箱" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">状态</label>
            <div class="layui-input-inline">
                <select id="active" name="active" lay-verify="required">
                    <option value="1" th:selected="true">开启</option>
                    <option value="2">禁用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">等级</label>
            <div class="layui-input-inline">
                <select id="grade" name="grade" lay-verify="required">
                    <option value="1" th:if="${adminGrade==1}" th:selected="true">超级管理员</option>
                    <option value="2" th:if="${adminGrade==1 || adminGrade==2}">省级管理员</option>
                    <option value="3" th:if="${adminGrade==1 || adminGrade==2 || adminGrade==3}">市级管理员</option>
                    <option value="4" th:if="${adminGrade==1 || adminGrade==2 || adminGrade==3 || adminGrade==4}">区级管理员</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label required">备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="description" name="description" placeholder="请输入备注信息" lay-verify="required|description"></textarea>
            </div>
        </div>
        <div class="layui-form-item base-finally">
            <button class="layui-btn" lay-submit lay-filter="add"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary base-close-dialog"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
</body>

<script th:replace="/sys/base :: scriptFragment"></script>
<!-- TODO RSA非对称加密 -->
<script th:src="@{/static/js/encrypt/jsencrypt.min.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['jquery', 'form'], function () {
        var $ = layui.jquery;
        var form = layui.form;

        /**
         * 提交表单
         */
        form.verify({
            account: function(value,item) {
                if (!/^[a-zA-Z][a-zA-Z0-9]{4,14}$/.test(value)) {
                    return "以字母开头的5-15位字母或数字组成"
                }
            },
            pass: function(value, item) {
                if (!/^[a-zA-Z]\w{5,9}$/.test(value)) {
                    return "以字母开头，长度在6~10之间，只能包含字母、数字和下划线";
                }
            },
            rePass: function (value, item) {
                var pass = $("input[name='pass']").val();
                if (value !== pass) {
                    return "两次密码输入不一致"
                }
            },
            userName: function(value,item) {
                if (!/^[\u4e00-\u9fa5]{2,5}$/.test(value)) {
                    return "只能输入2到5个汉字"
                }
            },
            description: function(value,item) {
                if (!/^(.|\n){1,225}$/.test(value)) {
                    return "备注信息由1-225位有效字符串组成"
                }
            }
        });
        form.on('submit(add)', function (data) { // TODO 提交表单
            data.field.createUserAccount = [[${#authentication.principal.username}]];
            data.field.pass = $.encryptRasLI(data.field.pass); // 按照公钥加密
            $.ajax({
                url: $.getProjectNameLI() + "/sys/admin/saveModify/insert",
                async: false,
                type: "post",
                dataType: "json",
                data: data.field,
                success: function (res) {
                    if (res.code === 0) {
                        layer.msg("添加成功", {time: 1500}, function () {
                            parent.layer.close(window.parent.layerIndex);
                            window.top.location.reload();
                        });
                    } else {
                        layer.msg(res.message, {time: 1500});
                    }
                }
                ,complete: function (xhr) {
                    $.ajaxCompleteLI(xhr);
                }
            });
            return false;
        });
    });
</script>

</html>